/*
    Superjuego Engine
    Copyright (C) 2011 Jorge Luís Cabral y Gabriel Ernesto Cabral

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Contact: baigosoft@hotmail.com
*/


#include "input.h"


SDL_Event event;
SE_input_state internal_input_state;

int SE_input_init(){

	return SDL_InitSubSystem(SDL_INIT_JOYSTICK);

}

SE_input_joystick *SE_input_open(int joyid){

	SE_input_joystick *joystick;
	SDL_JoystickEventState(SDL_ENABLE);

	joystick = SDL_JoystickOpen(joyid);
	
	return joystick;

}

int SE_input_enum(){
	return SDL_NumJoysticks();
}


SE_input_state *SE_input_poll(SE_input_joystick **joy){

	//internal_input_state.buttons=0;
	while(SDL_PollEvent(&event))
	{
	    switch(event.type)
	    {
		case SDL_JOYAXISMOTION:
	        	if( event.jaxis.axis == 0){
				internal_input_state.axis1x=event.jaxis.value;
				if(event.jaxis.value < -3200){
					internal_input_state.buttons&=~JOY_RIGHT;
					internal_input_state.buttons|=JOY_LEFT;
				}else if(event.jaxis.value > 3200){
					internal_input_state.buttons&=~JOY_LEFT;
					internal_input_state.buttons|=JOY_RIGHT;
				}else{
					internal_input_state.buttons&=~JOY_LEFT;
					internal_input_state.buttons&=~JOY_RIGHT;
				}

			}
			if( event.jaxis.axis == 1){
				internal_input_state.axis1y=event.jaxis.value;
				if(event.jaxis.value < -3200){
					internal_input_state.buttons&=~JOY_DOWN;
					internal_input_state.buttons|=JOY_UP;
				}else if(event.jaxis.value > 3200){
					internal_input_state.buttons&=~JOY_UP;
					internal_input_state.buttons|=JOY_DOWN;
				}else{
					internal_input_state.buttons&=~JOY_UP;
					internal_input_state.buttons&=~JOY_DOWN;
				}
			}
		break;
		case SDL_JOYBUTTONDOWN:
		    switch(event.jbutton.button)
		    {
		        case 0: 
		        	internal_input_state.buttons|=JOY_BUTTON0;break;
		        case 1: 
		        	internal_input_state.buttons|=JOY_BUTTON1;break;
		        case 2: 
		        	internal_input_state.buttons|=JOY_BUTTON2;break;
		        case 3: 
		        	internal_input_state.buttons|=JOY_BUTTON3;break;
		        case 4: 
		        	internal_input_state.buttons|=JOY_BUTTON4;break;
		        case 5: 
		        	internal_input_state.buttons|=JOY_BUTTON5;break;
		        case 6: 
		        	internal_input_state.buttons|=JOY_BUTTON6;break;
		        case 7:
				internal_input_state.buttons|=JOY_BUTTON7;break;
		    }break;
		case SDL_JOYBUTTONUP:
		    switch(event.jbutton.button)
		    {
		        case 0: 
		        	internal_input_state.buttons&=~JOY_BUTTON0;break;
		        case 1: 
		        	internal_input_state.buttons&=~JOY_BUTTON1;break;
		        case 2: 
		        	internal_input_state.buttons&=~JOY_BUTTON2;break;
		        case 3: 
		        	internal_input_state.buttons&=~JOY_BUTTON3;break;
		        case 4: 
		        	internal_input_state.buttons&=~JOY_BUTTON4;break;
		        case 5: 
		        	internal_input_state.buttons&=~JOY_BUTTON5;break;
		        case 6: 
		        	internal_input_state.buttons&=~JOY_BUTTON6;break;
		        case 7:
				internal_input_state.buttons&=~JOY_BUTTON7;break;
		    }break;
		case SDL_QUIT:
			internal_input_state.buttons|=SE_QUIT;break;
	    }
	}

	return &internal_input_state;
}
